package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.KnowledgeFood;
import com.itheima.service.KnowledgeFoodService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识库膳食库
 */
@RestController
@RequestMapping("/knowledgefood")
public class KnowledgeFoodController {
    @Reference
    private KnowledgeFoodService knowledgeFoodService;

    /**
     * 分页遍历膳食库所有数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('FOOD_QUERY')")
    public PageResult findPage (@RequestBody QueryPageBean queryPageBean){
        return knowledgeFoodService.findPage(queryPageBean);
    }

    /**
     * 新增膳食
     * @param knowledgeFood
     * @return
     */
    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('FOOD_ADD')")
    public Result add (@RequestBody KnowledgeFood knowledgeFood){
        try {
            knowledgeFoodService.add(knowledgeFood);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加膳食食品失败");
        }
        return new Result(true,"添加膳食食品成功!");
    }

    /**
     * 编辑修改膳食数据
     * @param knowledgeFood
     * @return
     */
    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('FOOD_EDIT')")
    public Result edit (@RequestBody KnowledgeFood knowledgeFood){
        try {
            knowledgeFoodService.edit(knowledgeFood);
            return new Result(true,"修改膳食数据成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改膳食数据失败");
        }
    }

    /**
     * 删除膳食数据
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @PreAuthorize("hasAuthority('FOOD_DELETE')")
    public Result delete(Integer id){
        try {
            knowledgeFoodService.delete(id);
            return new Result(true,"删除膳食数据成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除膳食数据失败");
        }
    }

    /**
     * 上传文件
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("excelFile")MultipartFile multipartFile){
        try {
            List<String[]> list = POIUtils.readExcel(multipartFile);
            ArrayList<KnowledgeFood> knowledgeFoods = new ArrayList<>();
            for (String[] strings : list) {
                //添加数据
                knowledgeFoods.add(new KnowledgeFood(strings[0],strings[1],strings[2],strings[3],strings[4]));
            }
            //调用业务层添加数据或者更新
            for (KnowledgeFood knowledgeFood : knowledgeFoods) {
                knowledgeFoodService.add(knowledgeFood);
            }
            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"上传失败");
        }
    }

    @RequestMapping("/findAll.do")
    public Result findAll(){
        try {
            List<KnowledgeFood> knowledgeFoods = knowledgeFoodService.findAll();
            return new Result(true,"获取膳食所有数据成功",knowledgeFoods);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"获取膳食所有数据失败");
        }

    }
}
